/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package UserInterface;

import Controller.Edge;
import Controller.Node;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.LinkedList;
import java.util.List;


/**
 *
 * @author subx
 */
public class MapCanvas extends Canvas {

    private Image map;
    private List<Shape> shapes;

    private Rectangle bounds;

    public MapCanvas() {
        setBackground(Color.WHITE);

        bounds = new Rectangle(693, 597);
        shapes = new LinkedList();
        map = null;
        
    }

    public void loadMap(String mapName){
        mapName = mapName.toString();
        MediaTracker tracker = new MediaTracker(this);
        
        this.map = Toolkit.getDefaultToolkit().createImage(mapName);
        
        tracker.addImage(this.map, 0);

        this.setBounds(new Rectangle(map.getWidth(this), map.getHeight(this)));
    }

    public void loadNodes(List<Node> nodes) {
        Shape s;

        if(nodes.isEmpty())
            return;
        
        for(Node n : nodes){
            s = new CircleShape("", n);
            addShape(s);
        }

        repaint();
    }

    public void loadEdges(List<List<Edge>> edges) {
        Shape e;

        for(List<Edge> l : edges){
            for(Edge n : l){
             e = new LineShape(n);
             addShape(e);
            }
        }

        repaint();
    }

    public void unload() {
        this.map = null;
        this.shapes.clear();
        
        repaint();
    }
    
    /**
     * Get the value of map
     *
     * @return the value of map
     */
    public Image getMap() {
        return map;
    }

    /**
     * Set the value of map
     *
     * @param map new value of map
     */
    public void setMap(Image map) {
        this.map = map;
    }

    public List<Shape> getShapes() {
        return shapes;
    }

    public void setShapes(List<Shape> shapes) {
        this.shapes = shapes;
    }

    @Override
    public Rectangle getBounds() {
        return this.bounds;
    }

    @Override
    public void setBounds(Rectangle rec) {
        this.bounds = rec;
    }

    public void addShape(Shape l){
        getShapes().add(l);
    }

    @Override
    public void paint(Graphics g) {
        try {

            if(map != null){
                //Desenha o mapa ao fundo
                g.drawImage(getMap(), 0, 0, this);
                setBounds(bounds);
            }
            
            //Desenha o grafo
            drawAll(shapes);
        }catch(RuntimeException e){
            e.printStackTrace();
        }
    }

    public void drawAll(List<? extends Shape> l) throws RuntimeException {
        if(getShapes() == null)
            throw new RuntimeException("Shapes is empty!");
        
        for(Shape e : shapes)
            e.draw(this);
    }
}
